{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Necessary condtions for quantum annealer\n",
    "### Install pyQUBO from Recruit Communications Co. Ltd.\n",
    "    pip install pyqubo\n",
    "### Install openJij from Jij Inc.  (startup from Tohoku University)\n",
    "    pip install openjij"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solve number partition problem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### import pyQUBO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyqubo  import Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Array is a class "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prepare spin variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 8\n",
    "vartype = \"SPIN\"\n",
    "x = Array.create(\"x\",shape=N,vartype=vartype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"x\" is name of variables  \n",
    "shape specifies the shape of variables as vector, matrix, or...  \n",
    "vartype selects -1 or 1 by \"SPIN\" and 0 or 1by \"BINARY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array([Spin(x[0]), Spin(x[1]), Spin(x[2]), Spin(x[3]), Spin(x[4]), Spin(x[5]), Spin(x[6]), Spin(x[7])])\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let generate integers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "n = np.linspace(1,N,N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5. 6. 7. 8.]\n"
     ]
    }
   ],
   "source": [
    "print(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define cost function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "E_cost = (np.sum(n*x))**2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compile the cost function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = E_cost.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get qubo matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "h,J, offset = model.to_ising()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### import openJij"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openjij as jij"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prepare simulation of quantum annealing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#simulated quantum annealing\n",
    "sampler = jij.SQASampler(beta=10.0, gamma=1.0, trotter=4, num_sweeps=100)\n",
    "#simulated annealing\n",
    "#sampler = jij.SASampler(num_sweeps=1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is done by quantum Monte-Carlo simulation  \n",
    "gamma = strength of quantum fluctuation  \n",
    "trotter = Trotter number  \n",
    "num_sweeps = length of MCS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's simulate quantum annealing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = sampler.sample_ising(h,J,num_reads=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### show results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] energy num_oc.\n",
      "0    -1   +1   -1   +1   +1   -1   +1   -1 -204.0       1\n",
      "1    -1   +1   +1   -1   -1   +1   +1   -1 -204.0       1\n",
      "3    +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "4    +1   -1   +1   -1   -1   +1   -1   +1 -204.0       1\n",
      "5    -1   +1   -1   +1   +1   -1   +1   -1 -204.0       1\n",
      "6    -1   +1   -1   +1   +1   -1   +1   -1 -204.0       1\n",
      "7    +1   +1   +1   +1   -1   -1   -1   +1 -204.0       1\n",
      "8    +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "10   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "15   -1   +1   +1   -1   +1   -1   -1   +1 -204.0       1\n",
      "17   -1   -1   +1   +1   +1   +1   -1   -1 -204.0       1\n",
      "18   +1   +1   +1   -1   +1   -1   +1   -1 -204.0       1\n",
      "19   -1   +1   -1   +1   +1   -1   +1   -1 -204.0       1\n",
      "20   -1   -1   +1   +1   +1   +1   -1   -1 -204.0       1\n",
      "21   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "24   +1   -1   -1   +1   -1   +1   +1   -1 -204.0       1\n",
      "25   +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "26   -1   +1   +1   -1   +1   -1   -1   +1 -204.0       1\n",
      "27   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "28   -1   -1   +1   +1   +1   +1   -1   -1 -204.0       1\n",
      "29   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "30   -1   +1   +1   -1   -1   +1   +1   -1 -204.0       1\n",
      "31   +1   -1   -1   +1   -1   +1   +1   -1 -204.0       1\n",
      "33   -1   -1   +1   -1   -1   -1   +1   +1 -204.0       1\n",
      "34   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "36   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "37   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "38   +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "39   +1   -1   -1   +1   -1   +1   +1   -1 -204.0       1\n",
      "40   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "41   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "42   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "43   +1   -1   +1   -1   -1   +1   -1   +1 -204.0       1\n",
      "44   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "46   -1   -1   -1   -1   +1   +1   +1   -1 -204.0       1\n",
      "48   -1   -1   -1   -1   +1   +1   +1   -1 -204.0       1\n",
      "49   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "50   -1   -1   +1   -1   -1   -1   +1   +1 -204.0       1\n",
      "51   +1   +1   +1   +1   -1   -1   -1   +1 -204.0       1\n",
      "52   +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "53   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "55   +1   -1   +1   -1   -1   +1   -1   +1 -204.0       1\n",
      "56   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "58   +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "59   -1   -1   +1   -1   -1   -1   +1   +1 -204.0       1\n",
      "60   -1   +1   +1   -1   -1   +1   +1   -1 -204.0       1\n",
      "62   -1   -1   +1   -1   -1   -1   +1   +1 -204.0       1\n",
      "64   -1   -1   +1   -1   -1   -1   +1   +1 -204.0       1\n",
      "66   +1   -1   -1   +1   -1   +1   +1   -1 -204.0       1\n",
      "67   +1   +1   -1   +1   +1   +1   -1   -1 -204.0       1\n",
      "68   -1   -1   -1   +1   -1   +1   -1   +1 -204.0       1\n",
      "69   -1   +1   +1   -1   +1   -1   -1   +1 -204.0       1\n",
      "70   +1   +1   +1   -1   +1   -1   +1   -1 -204.0       1\n",
      "71   -1   +1   +1   -1   +1   -1   -1   +1 -204.0       1\n",
      "72   -1   +1   -1   +1   +1   -1   +1   -1 -204.0       1\n",
      "73   -1   +1   +1   -1   -1   +1   +1   -1 -204.0       1\n",
      "74   +1   -1   +1   -1   -1   +1   -1   +1 -204.0       1\n",
      "75   +1   +1   +1   +1   -1   -1   -1   +1 -204.0       1\n",
      "76   +1   +1   -1   +1   +1   +1   -1   -1 -204.0       1\n",
      "77   +1   +1   -1   +1   +1   +1   -1   -1 -204.0       1\n",
      "78   -1   -1   +1   -1   -1   -1   +1   +1 -204.0       1\n",
      "79   +1   +1   +1   -1   +1   -1   +1   -1 -204.0       1\n",
      "81   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "82   +1   +1   -1   +1   +1   +1   -1   -1 -204.0       1\n",
      "83   +1   -1   +1   -1   -1   +1   -1   +1 -204.0       1\n",
      "84   -1   +1   +1   -1   +1   -1   -1   +1 -204.0       1\n",
      "85   -1   -1   +1   +1   +1   +1   -1   -1 -204.0       1\n",
      "88   -1   +1   +1   -1   +1   -1   -1   +1 -204.0       1\n",
      "89   +1   +1   -1   -1   -1   -1   +1   +1 -204.0       1\n",
      "90   -1   +1   -1   +1   +1   -1   +1   -1 -204.0       1\n",
      "91   +1   -1   +1   -1   -1   +1   -1   +1 -204.0       1\n",
      "92   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "93   +1   +1   -1   +1   +1   +1   -1   -1 -204.0       1\n",
      "94   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "97   +1   +1   +1   +1   -1   -1   -1   +1 -204.0       1\n",
      "98   +1   +1   +1   +1   -1   -1   -1   +1 -204.0       1\n",
      "99   +1   -1   -1   +1   +1   -1   -1   +1 -204.0       1\n",
      "9    -1   -1   -1   +1   -1   -1   +1   +1 -200.0       1\n",
      "14   +1   -1   +1   -1   +1   -1   -1   +1 -200.0       1\n",
      "22   +1   +1   -1   -1   -1   +1   -1   +1 -200.0       1\n",
      "32   -1   -1   -1   +1   +1   -1   -1   +1 -200.0       1\n",
      "35   -1   +1   +1   +1   -1   -1   -1   +1 -200.0       1\n",
      "54   -1   +1   +1   +1   -1   -1   -1   +1 -200.0       1\n",
      "57   -1   +1   +1   -1   -1   +1   -1   +1 -200.0       1\n",
      "61   -1   +1   -1   -1   -1   -1   +1   +1 -200.0       1\n",
      "65   +1   +1   +1   -1   -1   +1   +1   -1 -200.0       1\n",
      "80   +1   +1   -1   +1   +1   -1   +1   -1 -200.0       1\n",
      "86   -1   +1   -1   +1   -1   +1   +1   -1 -200.0       1\n",
      "96   +1   -1   -1   +1   -1   +1   -1   +1 -200.0       1\n",
      "2    -1   +1   +1   -1   +1   +1   -1   -1 -188.0       1\n",
      "11   -1   +1   +1   -1   -1   -1   +1   +1 -188.0       1\n",
      "12   -1   +1   -1   -1   +1   +1   +1   -1 -188.0       1\n",
      "13   +1   +1   +1   -1   -1   +1   -1   +1 -188.0       1\n",
      "95   -1   -1   +1   +1   -1   +1   +1   -1 -188.0       1\n",
      "16   -1   +1   +1   +1   +1   -1   +1   -1 -168.0       1\n",
      "23   -1   +1   -1   +1   -1   -1   +1   +1 -168.0       1\n",
      "45   +1   -1   +1   +1   +1   -1   -1   +1 -168.0       1\n",
      "47   +1   +1   +1   -1   -1   -1   +1   +1 -168.0       1\n",
      "63   -1   +1   +1   +1   -1   +1   -1   -1 -168.0       1\n",
      "87   +1   -1   -1   +1   +1   -1   +1   +1   -8.0       1\n",
      "['SPIN', 100 rows, 100 samples, 8 variables]\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### output samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "spin = response.record[\"sample\"][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18.0 36.0\n"
     ]
    }
   ],
   "source": [
    "temp = 0\n",
    "for k in range(N):\n",
    "    if spin[k]>0:\n",
    "        temp += n[k]\n",
    "print(temp,n.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
